var TreeAdd = function (layer, params, query, tree, isfull) {
    var self = this;
    self.title = params.title;
    self.url = params.url;
    self.width = params.width;
    self.height = params.height;
    self.getUrl = params.getUrl;
    self.layerIndex = null;
    self.openDialog = function () {
        var tempUrl = self.url;
        if (self.getUrl) {
            tempUrl = self.getUrl(self.url);
        }
        var idx = layer.open({
            type: 2,
            title: self.title,
            shadeClose: true,
            maxmin: true,
            skin: 'layui-layer-lan',
            shade: 0.8,
            offset: '20px',
            area: [self.width, self.height],
            content: tempUrl
        });
        self.layerIndex = idx;
        if (isfull == true) {
            layer.full(self.layerIndex);
        }
    };
    self.closeDialog = function () {
        layer.close(self.layerIndex);
    };
    self.addCompleted = function (node) {
        self.closeDialog();
        tree.add(node);
        query.reload();
    }
};

var TreeEdit = function (layer, params, query, tree, isfull) {
    var self = this;
    self.title = params.title;
    self.url = params.url;
    self.width = params.width;
    self.height = params.height;
    self.layerIndex = null;
    self.openDialog = function () {
        var items = query.getCheckedKeys();
        if (items.length < 1) {
            layer.msg("还没有选中记录！");
            return;
        }
        else if (items.length > 1) {
            layer.msg("只能选择一条记录！");
            return;
        }

        var id = items[0];
        self.layerIndex = layer.open({
            type: 2,
            title: self.title,
            shadeClose: true,
            skin: 'layui-layer-lan',
            shade: 0.8,
            area: [self.width, self.height],
            content: self.url + "/" + id
        });

        if (isfull == true) {
            layer.full(self.layerIndex);
        }
    }
    self.closeDialog = function () {
        layer.close(self.layerIndex);
    };
    self.editCompleted = function (node) {
        self.closeDialog();
        query.reload();
        tree.update(node);
    }
};

var TreeDelete = function (layer, query, tree, params) {
    var self = this;
    self.url = params.url;
    self.submit = function () {
        var data = query.getCheckedKeys();
        if (data.length < 1) {
            layer.msg("您没有选中记录");
            return;
        }
        layer.confirm('确认删除记录吗？', {
            btn: ['确认', '取消'],
            yes: function (index) {
                try {
                    layer.load();
                    var keydata = {keys: data};
                    ajaxPost(self.url, keydata, function (data) {
                        layer.closeAll('loading');
                        layer.msg("操作成功");
                        layer.close(index);
                        query.reload();
                        tree.delete(data);
                    }, function (error) {
                        layer.closeAll('loading');
                        layer.msg("操作失败");
                    });
                }
                catch (e) {
                    layer.closeAll('loading');
                }
            }
        });
    }
}

var TreeFormAdd = function (form, layer, add, params, formId, validFields) {
    var self = this;
    self.url = params.url;
    self.getData = params.getData;
    self.valid = new TreeFormValidation(formId, validFields);
    self.valid.initValid();
    self.close = function () {
        add.closeDialog();
    };
    self.submit = function () {
        try {
            if (self.valid.isValid()) {
                layer.load();
                var data = self.getData();
                ajaxPost(self.url, data, function (data) {
                    layer.closeAll('loading');
                    layer.msg("操作成功");
                    add.addCompleted(data);
                }, function (error) {
                    layer.closeAll('loading');
                    layer.msg("操作失败");
                });
            }
        }
        catch (e) {
            layer.closeAll('loading');
        }
    }
}

var TreeFormEdit = function (form, layer, edit, params, formId, validFields) {
    var self = this;
    self.url = params.url;
    self.getData = params.getData;
    self.valid = new TreeFormValidation(formId, validFields);
    self.valid.initValid();
    self.close = function () {
        edit.closeDialog();
    };
    self.submit = function () {
        try {
            if (self.valid.isValid()) {
                layer.load();
                var data = self.getData();
                ajaxPost(self.url, data, function (data) {
                    layer.closeAll('loading');
                    layer.msg("操作成功");
                    edit.editCompleted(data);
                }, function (error) {
                    layer.closeAll('loading');
                    layer.msg("操作失败");
                });
            }
        }
        catch (e) {
            layer.closeAll('loading');
        }
    }
}

var TreeFormValidation = function (formId, validFields) {
    var self = this;
    self.formId = formId;
    self.initValid = function () {
        $("#" + formId).bootstrapValidator({
            message: 'This value is not valid',
            feedbackIcons: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            fields: validFields
        });
    };
    self.isValid = function () {
        $("#" + formId).bootstrapValidator('validate');
        return $("#" + formId).data("bootstrapValidator").isValid();
    }
}

var loadCategories = function (divId, category, categoryId) {
    var url = contextRoot + category + "/getCategories" + "?categoryId=" + categoryId;
    $("#" + divId).load(url);
};

var loadDetailCategories = function (divId, category, categoryId, myId) {
    var url = contextRoot + category + "/getDetailCategories" + "?categoryId=" + categoryId + "&id=" + myId;
    $("#" + divId).load(url);
}